/**
 * \file
 */
#include "precompiled.hpp"
#include "kfs.hpp"
#include "kfs_impl.hpp"
#include "Exception.hpp"

using std::ios;  using std::fstream;


void kfs_disk_read(kfs::Impl& impl, uint64_t blockID, uint8_t* blockData)
{
    if(blockID >= impl.mb.numBlocks)
        throw Exception("Block number %d is too high", blockID);
    impl.f.clear();
    impl.f.exceptions(ios::badbit | ios::failbit | ios::eofbit);
    impl.f.seekg(blockID * impl.mb.blockNumBytes, ios::beg);
    impl.f.read((char*)blockData, impl.mb.blockNumBytes);
}


void kfs_disk_write(kfs::Impl& impl, uint64_t blockID, uint8_t const* blockData)
{
    if(blockID >= impl.mb.numBlocks)
        throw Exception("Block number %d is too high", blockID);
    impl.f.clear();
    impl.f.exceptions(ios::badbit | ios::failbit | ios::eofbit);
    impl.f.seekg(blockID * impl.mb.blockNumBytes, ios::beg);
    impl.f.write((char const*)blockData, impl.mb.blockNumBytes);
}
